/**
 * @Author : woon
 * @Timestamp : 2016-06-29
 */
var page = sm("do_Page");
var app = sm("do_App");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var circleUrl = require("url/circle");
var http = require("helper/http");
var open = require("helper/open");
var common = require("common");
var userUtil = require("helper/user");

var rootview = ui("$"); 
var el=ui("expandableListView1");

//声明UI变量
var listdata4G = mm("do_ListData");//分组数据
var listdata4Member = mm("do_ListData");//成员数据


var httpUrl4CircleMembers = circleUrl.url.member.members4circle;

var circleId = null;

//andriod返回键
page.on("back", function() {
    app.closePage();
});


/****************处理页面-头部-begin*****************/
var headerAout = ui("headerAout");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);

//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
	app.closePage();
});

var header = ui("header");
header.bindData(hashdata);
hashdata.addData({"labelTitle" : "圈子成员"});
header.refreshData();

//右上角-
var headerRight = ui(addheader + ".headerRight");
// 右上角添加+图标
var imgNext = ui(addheader + ".imgNext");
imgNext.source = "source://image/common/more.png";
imgNext.visible = true;

var menus=[{"template" : 0,"menuIcon":"source://image/menu/useradd.png","menuName" : "邀请新成员","type":"addMember"},
           {"template" : 0,"menuIcon":"source://image/setting/setting.png","menuName" : "设置","type":"setGroup"}];
var popmenu = rootview.add("menupop", "source://view/reuse/menu/menu.ui", 0, 0);
var popmenuf = ui("menupop");
headerRight.on("touch", function() {
	popmenuf.fire("popmenuSJ", menus);
});
/****************处理页面-头部-end*****************/

var canInviteUser = false;
page.on("loaded", function(){
	//读取当前页面的传入参数
	circleId = page.getData().circleId;
	bindMemberData();
});

page.on("result", function(data){
	if(data.invite_sucess){
		nf.toast("你已成功发出邀请!");
	}
});


//全部刷新数据
function bindMemberData(){
	var managerList = [];//管理员(含圈主)
	var memberList = [];//普通成员
	http.post(httpUrl4CircleMembers,{circleId:circleId},function(data){
		var members = data.members;
		canInviteUser = data.canInviteUser;//是否可以邀请新成员
		if(members){
			for(var i=0;i<members.length;i++){
			  if(members[i].type == 3){
				  memberList.push(members[i]);
			  }else{
				  managerList.push(members[i]);
			  }
			}
			var groupDatas = [];
			groupDatas.push({"name":"圈主、管理员("+managerList.length+"人)"});
			groupDatas.push({"name":"成员("+memberList.length+"人)"});
			
			listdata4G.addData(groupDatas);
			listdata4Member.addData([managerList,memberList]);//加载成员列表
			el.bindItems(listdata4G,listdata4Member);
			el.expandGroup([0]);//展开管理员
			el.expandGroup([1]);//展开成员
		}
	});
}

//点击菜单选项
popmenuf.on("popmenuItem", function(data) {
	var touchIndex = parseInt(data);
	var actionType = menus[touchIndex].type;
	
	if(actionType=="addMember"){//邀请新成员
		if(userUtil.loginToken()===""){
			nf.toast("您还没有登录哦.");
			return;
		}
		if(canInviteUser){
			common.open("source://view/circle/member/invite/invite2circle.ui",{"circleId":circleId});
		}else{
			nf.toast("您不是该圈子成员,不能邀请!");
		}
	}else if(actionType=="setGroup"){//设置
		if(canInviteUser){
			common.open("source://view/circle/member/manage/gchatSet.ui",{"circleId":circleId});
		}else{
			nf.toast("您不是该圈子成员!");
		}
	}
	popmenuf.fire("popmenuClose");
});
